package com.example.nonacademic_education.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import com.example.nonacademic_education.constant.Constant;
import com.example.nonacademic_education.data.request.LoginR;
import com.example.nonacademic_education.entity.Admin;
import com.example.nonacademic_education.service.impl.AdminServiceImpl;
import com.example.nonacademic_education.service.impl.StudentServiceImpl;
import com.example.nonacademic_education.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author xinmiao
 * @description
 * @date 2023/3/28 14:45
 */
@RestController
@RequestMapping("admin")
public class AdminController {
    @Resource
    private AdminServiceImpl adminService;
    @Resource
    private StudentServiceImpl studentService;
    @PostMapping("login")
    @SaIgnore
    public R login(@RequestBody LoginR admin, HttpServletRequest request){
        return adminService.login(admin,request);
    }
    @GetMapping("getIdAndName")
    @SaCheckRole(Constant.NORMAL_ADMIN)
    public R getAdminByName(String name,Long departmentId){
        return adminService.getAdminByName(name,departmentId);
    }
    @SaCheckPermission("admin.query")
    @GetMapping("getList")
    public R getList(Integer page, Integer size, String name,Long departmentId) {
        return adminService.getList(page,size,name,departmentId);
    }
    @SaCheckPermission("admin.add")
    @PostMapping("addOne")
    public R addOne(@RequestBody Admin admin) {
        return adminService.addOne(admin);
    }
    @SaCheckPermission("admin.update")
    @PutMapping("updateOne")
    public R updateOne(@RequestBody Admin admin) {
        return adminService.updateOne(admin);
    }
    @PutMapping("updateOneself")
    @SaCheckRole(Constant.NORMAL_ADMIN)
    public R updateOneself(@RequestBody Admin admin) {
        return adminService.updateOneself(admin);
    }
    @PutMapping("resetPassword")
    @SaCheckRole(Constant.NORMAL_ADMIN)
    public R resetPassword(@RequestBody LoginR loginR) {
        return adminService.resetPassword(loginR);
    }
    //@PutMapping("resetAdminPassword")
    @SaCheckRole(Constant.SUPER_ADMIN)
    public R resetAdminPassword(@RequestBody LoginR loginR) {
        return adminService.resetPassword(loginR);
    }
    @SaCheckPermission("admin.delete")
    @DeleteMapping("deleteOne")
    public R deleteOne(Long id) {
        return adminService.deleteOne(id);
    }
    @GetMapping("getPersonalInfo")
    //@SaCheckRole(Constant.NORMAL_ADMIN)
    @SaCheckRole(Constant.STUDENT)
    public R getPersonalInfo() {
        return adminService.getPersonalInfo();
    }
    // TODO: 2023/5/9 问卷功能
    //todo:启用、禁用账号，办学信息表的导入导出,账号封禁，学员密码找回（这个暂时不做，因为没有确定找回的条件，如短信验证）
    @PutMapping("resetAdminPassword/{username}")
    @SaCheckRole(Constant.SUPER_ADMIN)
    public R resetAdminPassword(@PathVariable String username) {
        return adminService.resetAdminPassword(username);
    }
    @PutMapping("resetStudentPassword/{identityCardId}")
    @SaCheckRole(Constant.NORMAL_ADMIN)
    public R resetStudentPassword(@PathVariable String identityCardId) {
        return studentService.resetStudentPassword(identityCardId);
    }
    @PutMapping("disableAccount")
    @SaCheckRole(Constant.NORMAL_ADMIN)
    public R disableAccount(String username) {
        return adminService.enableAccount(username,0);
    }
    @PutMapping("enableAccount")
    @SaCheckRole(Constant.NORMAL_ADMIN)
    public R enableAccount(String username) {
        return adminService.enableAccount(username,1);
    }
}
